package cn.lyvust.map;

import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.List;

/**
 * Description:
 * date: 2021-6-1 10:06
 *
 * @author Guo_guo
 * @since JDK 1.8
 */
public class LuckyMap extends JFrame {
    int lines = 0;
    int index = 0;
    String luckyName = null;
    Map<String, Integer> nameMap = new HashMap<>();
    public static void main(String[] args) {
        new LuckyMap();
    }

    public LuckyMap() {
        getNames();
        this.setTitle("临科抽奖小程序");
        this.setSize(300, 200);
        this.setResizable(false);
//        this.getContentPane().setBackground(Color.RED);
        this.setLocationRelativeTo(null);
        //设置自定义布局,否则按钮会被干预,显示位置不符合预期
        this.setLayout(null);
        this.init();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void init() {
        JButton jbStart = new JButton("开始");
        JButton jbEnd = new JButton("结束");
        //设置按钮的位置和大小
        jbStart.setBounds(32, 107, 93, 37);
        jbEnd.setBounds(165, 107, 93, 37);
        //将按钮显示到窗体上
        this.add(jbStart);
        this.add(jbEnd);

        //设置文本显示到窗体上
        JLabel jlStart = new JLabel("开始");
        jlStart.setFont(new Font(Font.DIALOG, Font.BOLD, 30));
        jlStart.setBounds(70, 45, 120, 30);
        this.add(jlStart);

        //定义一个定时器,每100毫秒执行一次
        Timer timer = new Timer(10, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int random = 0;
                Random rand = new Random();
                if (lines <= 0) {
                    return;
                }
                random = rand.nextInt(lines);
                List<String> namesTemp = new ArrayList<>();
                for (Map.Entry<String, Integer> entry : nameMap.entrySet()) {
                    if (entry.getValue() == 0) {
                        namesTemp.add(entry.getKey());
                    }
                }
                if (index >= namesTemp.size() && lines != 0) {
                    index = index%lines;
                }
                luckyName = namesTemp.get(index);
                jlStart.setText(luckyName);
                index += random;
            }
        });

        //按钮的响应函数/动作
        jbStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.start();
            }
        });
        //点击结束按钮的响应函数
        jbEnd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
                resetNameMap();
            }
        });
    }
    public void resetNameMap() {
        //利用迭代器循环遍历，移除抽中的姓名，防止再次被抽中
        Iterator iter = nameMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry =  (Map.Entry) iter.next();
            if (entry.getKey() != null && entry.getKey().equals(luckyName)) {
                //重置抽中姓名对应的value为1,0：未抽中, 1:已抽中
                entry.setValue(1);
                //统计等于零的个数,防止数组越界
                lines = (int) nameMap.entrySet().stream().filter(x -> x.getValue() == 0).count();
                break;
            }
        }
    }
    //读取学生的全部名单信息
    public void getNames() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("names.txt"));
            String name = br.readLine();
            while (name != null) {
                //保存到map里面
                nameMap.put(name, 0);
                name = br.readLine();
                lines ++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
